<?php
declare(strict_types=1);
namespace App\ZxAdmin\Sys\Curd;
use App\Controller;
use Field\SysCurdConst;
use Framework\Support\Request;
use Swoole\Http\Response;

// hook controller_zx_admin_sys_sys_curd_use.php

/**
 * @title   CURD记录
 * @auth    true
 * @login   true
 * @menu    true
 * @component   vlist/index.vue
 * @icon    sc-icon-bug-line
 * @active  true
 * @affix   false
 * @rank    10
 */
class SysCurd extends Controller
{
    // hook controller_zx_admin_sys_sys_curd_public_start.php

    // make public SysCurd ZxAdmin\Sys

    public function init(string $controller, array $route, string $action, Request $request, Response $response): void
    {
        parent::init($controller, $route, $action, $request, $response); // TODO: Change the autogenerated stub
        if (__LV__ != 'dev') {
            $this->response('0001', [], "本地开发模式才可以操作");
        }
    }

    /**
     * @title   列表配置
     */
    public const Conf = [
        'pageSize' => 25,
        'pageSizes' => [10,20,25,50,100,500,1000,10000],
        'remoteSort' => 0,
        'remoteFilter' => 0,
        'remoteSummary' => 0,
        'hidePagination' => 1,
        'hideRefresh' => 0,
        'hideSetting' => 1,
        'hideDo' => 1,
        'add' => 0,
        'start' => 0,
        'stop' => 0,
        'delete' => 1,
        'import' => 0,
        'export' => 0,

    ];

    // hook controller_zx_admin_sys_sys_curd_public_end.php

    /**
     * @title  下发配置
     * @rank    0
     */
    public function action_Conf_PUT()
    {
        // hook controller_zx_admin_sys_sys_curd_conf_start.php

        $Columns = [
            [
                'label' => "操作",
                'type' => "button",
                'prop' => "CurdID",
                'width' => "50",
                'options' => [
                    ['Name'=>'生成','Field'=>'CurdID','width'=>"80%",'Do'=>'Ctrl'],
                ],
            ],
            [
                'type' => "input",
                'label' => "Node",
                'prop' => "Node",
                'editable'=>1,
                'width' => "200"
            ],
            [
                'type' => "input",
                'label' => "标题",
                'prop' => "Title",
                'editable'=>1,
                'width' => "300"
            ],
            [
                'type' => "icon",
                'label' => "图标",
                'prop' => "Icon",
                'editable'=>1,
                'width' => "50px"
            ],
            [
                'type' => "input",
                'label' => "排序",
                'prop' => "Rank",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "dict",
                'label' => "前端模板",
                'prop' => "Component",
                'width' => "100",
                'editable'=>1,
                'dict' => 'Component'
            ],
            [
                'type' => "dict",
                'label' => "授权",
                'prop' => "Auth",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "dict",
                'label' => "登录",
                'prop' => "Login",
                'editable'=>1,
                'width' => "60"
            ],
            [
                'type' => "dict",
                'label' => "菜单",
                'prop' => "Menu",
                'editable'=>1,
                'width' => "60"
            ],[
                'type' => "textarea",
                'label' => "配置",
                'prop' => "Conf",
                'editable'=>1,
                'width' => "60"
            ],[
                'type' => "textarea",
                'label' => "列配置",
                'prop' => "Columns",
                'editable'=>1,
                'width' => "60"
            ],[
                'type' => "textarea",
                'label' => "新增数据",
                'prop' => "AddForm",
                'editable'=>1,
                'width' => "60"
            ],[
                'type' => "textarea",
                'label' => "筛选数据",
                'prop' => "Filter",
                'editable'=>1,
                'width' => "60"
            ],[
                'type' => "tool",
                'label' => "表头工具",
                'prop' => "Tools",
                'editable'=>1,
                'width' => "60"
            ],[
                'type' => "tool",
                'label' => "行按钮",
                'prop' => "Buttons",
                'editable'=>1,
                'width' => "60"
            ],[
                'type' => "right",
                'label' => "右键菜单",
                'prop' => "MenuItems",
                'editable'=>1,
                'width' => "60"
            ],
        ];
        $Tools=[
            ['Name' => '模板', 'Field' => 'Component', 'Do' => 'Dict', 'Dict' => 'Component'],
            ['Name' => '授权', 'Field' => 'Auth', 'Do' => 'Dict', 'Dict' => 'Status'],
            ['Name' => '登录', 'Field' => 'Login', 'Do' => 'Dict', 'Dict' => 'Status'],
            ['Name' => '菜单', 'Field' => 'Menu', 'Do' => 'Dict', 'Dict' => 'Status'],
        ];

        // make conf SysCurd ZxAdmin\Sys
        // hook controller_zx_admin_sys_sys_curd_conf_end.php
        $this->response('0000', ['Conf' => self::Conf, 'Filter' => [], 'Tools' =>$Tools, 'Columns' => $Columns]);
    }

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin_sys_sys_curd_index_start.php
        $page = $this->request->param('page', 0);
        $size = $this->request->param('limit', 0);
        $field = $this->request->param('field', '');
        $sort = $this->request->param('sort', '');
        $order = [];
        !empty($field) && $order[$field] = $sort == 'asc' ? -1 : 1;
        $cond = [];

        $data = $this->SysCurd->GetList($cond, $order, $page, $size);

        foreach ($data['data'] as &$row) {

            //unset($row['PassWord'],$row['Salt']);
        }

        // hook controller_zx_admin_sys_sys_curd_index_end.php
        $this->response('0000', $data);
    }

    /**
     * @title  下发编辑数据
     * @rank    0
     */
    public function action_Edit_GET()
    {
        $CurdID = $this->request->param('CurdID', 0);
        $this->CheckEmpty([$CurdID], ['必要参数']);
        $data = $this->SysCurd->read_by_index($CurdID);
        empty($data['CurdID']) && $this->response('0003', [], '数据不存在');

        $data['Tools'] = xn_json_decode($data['Tools']);
        $data['Filter'] = xn_json_decode($data['Filter']);
        $data['Conf'] = xn_json_decode($data['Conf']);
        $data['Columns'] = xn_json_decode($data['Columns']);
        $data['AddForm'] = xn_json_decode($data['AddForm']);
        $data['Buttons'] = xn_json_decode($data['Buttons']??"");
        $data['MenuItems'] = xn_json_decode($data['MenuItems']??"");

        $this->response('0000', ['data' => $data]);
    }

    /**
     * @title  保存配置
     * @rank    0
     */
    public function action_Save_POST()
    {
        $req = xn_json_decode( $this->request->param('data'));
        empty($req) && $this->response('0003',[],'数据不能为空');
        $CurdID = (int)$req['CurdID'];

        $this->CheckEmpty([$CurdID], ['必要参数']);
        $data = $this->SysCurd->read_by_index($CurdID);
        empty($data['CurdID']) && $this->response('0003', [], '数据不存在');

        $data['Node'] = $req['Node'];
        $data['Title'] = $req['Title'];
        $data['Auth'] = $req['Auth'];
        $data['Login'] = $req['Login'];
        $data['Menu'] = $req['Menu'];
        $data['Component'] = $req['Component'];
        $data['Rank'] = $req['Rank'];
        $data['Icon'] = $req['Icon'];
        $data['Conf'] = xn_json_encode($req['Conf']);
        $data['Columns'] = xn_json_encode($req['Columns']);
        $data['Filter'] = xn_json_encode($req['Filter']);
        $data['Tools'] = xn_json_encode($req['Tools']);
        $data['AddForm'] = xn_json_encode($req['AddForm']);
        $data['Buttons'] = xn_json_encode($req['Buttons']);
        $data['MenuItems'] = xn_json_encode($req['MenuItems']);
        $r = $this->SysCurd->updateByPk($CurdID, $data);
        $this->response($r ? '0000' : '0001', [], $r ? '保存成功' : '保存失败');
    }

    /**
     * @title  保存配置
     * @rank    0
     */
    public function action_Make_POST()
    {
        $CurdID = $this->request->param('CurdID', 0);
        $this->CheckEmpty([$CurdID], ['必要参数']);
        $data = $this->SysCurd->read_by_index($CurdID);
        empty($data['CurdID']) && $this->response('0003', [], '数据不存在');
        $r = false;
        $node = $data['Node'];
        $tplname = $data['Tpl'];
        empty($tplname) && $tplname = 'Controller';
        empty($node) && $this->response('0001', [], '节点不能为空');
        $nodes = explode('\\', $node, 4);
        count($nodes) < 2 && $this->response('0001', [], '路径不能为空');
        if (!file_exists(__PLUDIR__.'Curd/Tpl/' . $tplname . '.tpl')) {
            $this->response('0001', [], '模板文件不存在');
        }
        $tpl_tmp = file_get_contents(__PLUDIR__.'Curd/Tpl/' . $tplname . '.tpl');
        $path = __APPDIR__ . 'Controller/';
        $classname = array_pop($nodes);
        $namespace = $nodes[0] . (!empty($nodes[1]) ? '\\' . $nodes[1] : '') . (!empty($nodes[2]) ? '\\' . $nodes[2] : '') . (!empty($nodes[3]) ? '\\' . $nodes[3] : '');
        $class_name = str_replace('__', '_', HumpToLowercase(str_replace('\\', '_', $namespace . $classname)));
        $refColumns = [];
        for ($j = 1; $j <= 5; $j++) {
            if(empty($data['Table' . $j])){
                continue;
            }
            empty($refColumns[$data['Table' . $j]]) && $refColumns[$data['Table' . $j]] = new \ReflectionClass("\\Field\\" . $data['Table' . $j] . 'Columns');
        }
        $table1 = $data['Table1'];
        $index = constant("\\Field\\" . $table1 . 'Const::index');
        $pk = constant("\\Field\\" . $table1 . 'Const::pk');

        $Conf = xn_json_decode($data['Conf']);
        $Columns = xn_json_decode($data['Columns']);
        $AddForm = xn_json_decode($data['AddForm']);
        $Filter = xn_json_decode($data['Filter']);
        $Tools = xn_json_decode($data['Tools']);
        $Buttons = xn_json_decode($data['Buttons']);
        $MenuItems = xn_json_decode($data['MenuItems']);

        $index_foreach = '';
        $index_foreach_kv = '';
        $index_foreach_kv_cond = '';
        //$index_foreach_name=[];
        $i = count($index);
        foreach ($index as $row) {
            $property = $refColumns[$data['Table1']]->getProperty($row);
            $type = $property->getType()->getName();
            $index_foreach .= "		\$cond['$row']=\$this->request->param('$row'," . ($type == 'int' ? 0 : "") . ");\r\n";

            if($type=='int'){
                if ($i == 1) {
                    $index_foreach_kv .= "        isset(\$row['" . $row . "']) && \$cond['" . $row . "'][]=intval(\$row['" . $row . "']);\r\n";
                    $index_foreach_kv_cond .= "	\$cond_arr['" . $row . "']=intval(\$row['" . $row . "']);\r\n";
                } else {
                    $index_foreach_kv .= "        isset(\$cond['" . $row . "'])	&& \$cond['" . $row . "']=intval(\$row['" . $row . "']);\r\n";
                    $index_foreach_kv_cond .= "   isset(\$cond['" . $row . "'])	&& \$cond['" . $row . "']=intval(\$row['" . $row . "']);\r\n";
                }
            }else{
                if ($i == 1) {
                    $index_foreach_kv .= "        isset(\$row['" . $row . "']) && \$cond['" . $row . "'][]=\$row['" . $row . "'];\r\n";
                    $index_foreach_kv_cond .= "	\$cond_arr['" . $row . "']=\$row['" . $row . "'];\r\n";
                } else {
                    $index_foreach_kv .= "        isset(\$cond['" . $row . "'])	&& \$cond['" . $row . "']=\$row['" . $row . "'];\r\n";
                    $index_foreach_kv_cond .= "   isset(\$cond['" . $row . "'])	&& \$cond['" . $row . "']=\$row['" . $row . "'];\r\n";
                }
            }
        }



        $field_fields = [];
        $field_fields_kv = [];
        $column_field_switch_fmt = '';
        $column_foreach_with = '';
        $column_foreach_with_fmt = '';
        $column_field_switch_fmt_int = [];
        $column_field_switch_fmt_other = [];
        $column_batch_field_switch_fmt_int = [];
        $column_batch_field_switch_fmt_other = [];

        $field_batch_fields=[];
        $field_batch_fields_kv=[];

        foreach ($Columns as $row) {
            $k = $row['table'];
            $tb = explode('.',$row['table'])[0];
            $property = $refColumns[$tb]->getProperty($row['prop']);
            $type = $property->getType()->getName();
            if (!empty($row['batch'])) {
                $field_batch_fields[] = $row['prop'];
                empty($field_batch_fields_kv[$k]) && $field_batch_fields_kv[$k] = [];
                $field_batch_fields_kv[$k][] = $row;

                if ($type == 'int') {
                    $column_batch_field_switch_fmt_int[] = $row['prop'];
                } else {
                    $column_batch_field_switch_fmt_other[] = $row['prop'];
                }
            }
            if (!empty($row['field'])) {
                $field_fields[] = $row['prop'];
                empty($field_fields_kv[$k]) && $field_fields_kv[$k] = [];
                $field_fields_kv[$k][] = $row;

                if ($type == 'int') {
                    $column_field_switch_fmt_int[] = $row['prop'];
                } else {
                    $column_field_switch_fmt_other[] = $row['prop'];
                }
            }
            empty($echo_field[$k]) && $echo_field[$k] = [];
            $echo_field[$k][] = $row['prop'];
            $tb = explode('.', $k);

            if (!empty($join[$row['prop']]) && $tb[1] > 1) {
                $column_foreach_with .= "		\$rip{$tb[1]} = arrlist_values(\$data['data'],'{$row['prop']}');\r\n";
                $porpsp = explode('.', $join[$row['prop']]);
                $column_foreach_with .= "		\${$tb[0]}{$tb[1]} =!empty(\$rip{$tb[1]}) ? \$this->{$porpsp[0]}->select(['{$porpsp[1]}'=>\$rip{$tb[1]}]):[];\r\n";
                //$table_set[rtrim($k,'.1234')]=1;
            }
            if ($tb[1] > 1 && !empty($porpsp)) {
                $column_foreach_with_fmt .= "		    \$row['{$k}.{$row['prop']}'] =!empty(\${$tb[0]}{$tb[1]}[\$row['{$porpsp[1]}']]) ? \${$tb[0]}{$tb[1]}[\$row['{$porpsp[1]}']]['{$row['prop']}']:'';\r\n";
            }
        }
        if(!empty($Buttons) && count($Buttons)>0){
            $but = [
                'label' => "操作",
                'type' => "button",
                'options' => $Buttons,
            ];
            array_unshift($Columns,$but);
        }

        $add_foreach = '';
        foreach ($AddForm as $row) {
            $tb = explode('.',$row['table'])[0];
            $property = $refColumns[$tb]->getProperty($row['prop']);
            $type = $property->getType()->getName();
            $prop = $row['prop'];
            $add_foreach .= "		\$data['$prop']=\$this->request->param('$prop'," . ($type == 'int' ? 0 : "") . ");\r\n";
            if(!empty($row['validator'])){
                empty($row['message']) && $row['message']='验证失败';
                $required='';
                empty($row['required']) && $required .="!empty(\$data['{$prop}']) && ";
                $add_foreach .="		{$required}!preg_match('/{$row['validator']}/', \$data['{$prop}']) && \$this->response('0003',[],'{$row['message']}');\r\n";
            }
        }


        if ($column_field_switch_fmt_int) {
            foreach ($column_field_switch_fmt_int as $row) {
                $column_field_switch_fmt .= "         case '{$row}':\r\n";
            }
            $column_field_switch_fmt .= "             \$update[\$field] = intval(\$value);\r\n            break;\r\n";
        }
        if ($column_field_switch_fmt_other) {
            foreach ($column_field_switch_fmt_other as $row) {
                $column_field_switch_fmt .= "         case '{$row}':\r\n";
            }
            $column_field_switch_fmt .= "             \$update[\$field] = \$value;\r\n            break;\r\n";
        }
        $column_batch_field_switch_fmt='';
        if ($column_batch_field_switch_fmt_int) {
            foreach ($column_batch_field_switch_fmt_int as $row) {
                $column_batch_field_switch_fmt .= "         case '{$row}':\r\n";
            }
            $column_batch_field_switch_fmt .= "             \$update[\$field] = intval(\$value);\r\n            break;\r\n";
        }
        if ($column_batch_field_switch_fmt_other) {
            foreach ($column_batch_field_switch_fmt_other as $row) {
                $column_batch_field_switch_fmt .= "         case '{$row}':\r\n";
            }
            $column_batch_field_switch_fmt .= "             \$update[\$field] = \$value;\r\n            break;\r\n";
        }


        $field_fields = array_unique($field_fields);
        $field_batch_fields = array_unique($field_batch_fields);
        $fields = constant("\\Field\\" . $table1 . 'Const::fields');
        $edit_foreach="";

        if (empty($add_foreach)) {
            $pattern = "/\/\/need_add_start(.*?)\/\/need_add_end/s";
            $tpl_tmp = preg_replace($pattern, "", $tpl_tmp);
        }
        if (empty($edit_foreach)) {
            $pattern = "/\/\/need_edit_start(.*?)\/\/need_edit_end/s";
            $tpl_tmp = preg_replace($pattern, "", $tpl_tmp);
        }



        if (empty($field_fields)) {
            $pattern = "/\/\/need_field_start(.*?)\/\/need_field_end/s";
            $tpl_tmp = preg_replace($pattern, "", $tpl_tmp);
        }
        if (empty($field_batch_fields)) {
            $pattern = "/\/\/need_update_start(.*?)\/\/need_update_end/s";
            $tpl_tmp = preg_replace($pattern, "", $tpl_tmp);
        }

        if (empty($Conf['import'])) {
            $pattern = "/\/\/need_import_start(.*?)\/\/need_import_end/s";
            $tpl_tmp = preg_replace($pattern, "", $tpl_tmp);
        }
        if (empty($Conf['export'])) {
            $pattern = "/\/\/need_export_start(.*?)\/\/need_export_end/s";
            $tpl_tmp = preg_replace($pattern, "", $tpl_tmp);
        }


        $filter_foreach = '';
        $modes = ['>', '>=', '<', '<=','><','LI','LL','RL','NL'];

        foreach ($Filter as $row) {

            empty($row['mode']) && $row['mode'] = '';
            empty($row['type']) && $row['type'] = '';
            $tb = explode('.',$row['table'])[0];
            $property = $refColumns[$tb]->getProperty($row['prop']);
            $type = $property->getType()->getName();
            $def_value = '';
            $type=='int' && $def_value='0';
            if(!empty($row['mode']) && $row['component']=='radioGroup'){
                $def_value='-1';
            }
            $ay = ($row['mode'] == 'IN' || $row['type']=='dates' || $row['type']=='years' || $row['type']=='datetimerange' || $row['type']=='daterange' || $row['type']=='monthrange' ) ? ',['.$def_value.']' : ','.$def_value;
            $like = in_array($row['mode'], $modes) ? '[\'' . $row['mode'] . '\']' : '';
            $filter_foreach .= "		\$" . $row['prop'] . "=\$this->request->param('" . $row['prop'] . "'" . $ay . ");\r\n";
            //required
            //validator

            if(!empty($row['validator'])){
                $filter_foreach .="		!preg_match('{$row['validator']}', \${$row['prop']}) && \$this->response('0003',[],'{$row['message']}');\r\n";
            }

            if(!empty($row['mode']) && $row['component']=='radioGroup'){
                $filter_foreach .= "		\$" . $row['prop'] . "!=-1 && \$cond['" . $row['prop'] . "']$like=\$" . $row['prop'] . ";\r\n";
            }else{
                if($row['type']=='daterange' || $row['type']=='dates'){
                    $filter_foreach .= "		!empty(\$" . $row['prop'] . ") && \$cond['" . $row['prop'] . "']$like=[\$".$row['prop'].'[0].\' 00:00:00\',$'.$row['prop']."[1].' 23:59:59'];\r\n";
                }else{
                    $filter_foreach .= "		!empty(\$" . $row['prop'] . ") && \$cond['" . $row['prop'] . "']$like=\$" . $row['prop'] . ";\r\n";
                }
            }
            //$index_foreach_name[]=$row;
        }


        $replace_tmp = [
            '__NameSpace__' => $namespace,
            '__CLASSNAME__' => $classname,
            '__Table__' => $data['Table1'],
            '_class_name_' => $class_name,
            '$title$' => $data['Title'],
            '$auth$' => $data['Auth'] ? 'true' : 'false',
            '$login$' => $data['Login'] ? 'true' : 'false',
            '$menu$' => $data['Menu'] ? 'true' : 'false',
            '$component$' => $data['Component'] ?: 'vlist/index.vue',
            '$icon$' => $data['Icon'] ?: 'el-icon-menu',
            '$rank$' => $data['Rank'] ?: 99,
            '$pk$' => $pk,
            '$index_foreach$' => trim($index_foreach),
            '$index_foreach_kv$' => trim($index_foreach_kv),
            '$index_foreach_kv_cond$' => trim($index_foreach_kv_cond),
            '$field_fields$' => trim(arrayToString($field_fields)),
            '$field_batch_fields$' => trim(arrayToString($field_batch_fields)),
            '$column_field_switch_fmt$' => trim($column_field_switch_fmt),
            '$column_foreach_with$' => trim($column_foreach_with),
            '$column_foreach_with_fmt$' => trim($column_foreach_with_fmt),
            '$filter_foreach$' => trim($filter_foreach),
            '$add_foreach$' => trim($add_foreach),
            '$edit_foreach$' => trim($edit_foreach),
            '$column_batch_field_switch_fmt$' => trim($column_batch_field_switch_fmt),

            '//need_add_start' => '',
            '//need_add_end' => '',
            '//need_edit_start' => '',
            '//need_edit_end' => '',
            '//need_delete_start' => '',
            '//need_delete_end' => '',
            '//need_status_start' => '',
            '//need_status_end' => '',
            '//need_option_start' => '',
            '//need_option_end' => '',
            '//need_import_start' => '',
            '//need_import_end' => '',
            '//need_export_start' => '',
            '//need_export_end' => '',
            '//need_field_start' => '',
            '//need_field_end' => '',
            '//need_update_start' => '',
            '//need_update_end' => '',

            '$conf$' => arrayToString($Conf),
            '$columns$' => arrayToString($Columns),
            '$addform$' => arrayToString($AddForm),
            '$filter$' => arrayToString($Filter),
            '$tools$' => arrayToString($Tools),
            '.1' => ''
        ];
        $tpl_tmp = str_replace(array_keys($replace_tmp), array_values($replace_tmp), $tpl_tmp);
        $path .= str_replace('\\', '/', $namespace) . '/';
        is_dir($path) or mkdir($path, 0755, TRUE);
        //$ext = file_exists($path . $classname . '.php') ? '.tmp' : '.php';
        $ext = '.php';
        $r = file_put_contents_try($path . $classname . $ext, $tpl_tmp);

        $this->response($r ? '0000' : '0001', [], $r ? '保存成功' : '保存失败');
    }


    /**
     * @title   删除数据
     * @rank    50
     */
    public function action_Index_Delete()
    {
        // hook controller_zx_admin_sys_sys_curd_delete_start.php
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$data], ['操作数据为空']);
        $r = $this->SysCurd->deleteByIndex($data);
        // hook controller_zx_admin_sys_sys_curd_delete_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zx_admin_sys_sys_dict_field_post_start.php
        $cond = [];
        $cond[SysCurdConst::pk] = $this->request->param(SysCurdConst::pk, '');
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field], ['待修改字段']);
        $field_fields = ['Node', 'Icon', 'Title', 'Component', 'Auth', 'Login', 'Menu', 'Rank','Conf','Columns','AddForm','Filter','Tools','Buttons','MenuItems'];
        !in_array($field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($field, SysCurdConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($field) {
            case 'Node':
            case 'Icon':
            case 'Title':
            case 'Component':
            case 'Conf':
            case 'Columns':
            case 'AddForm':
            case 'Filter':
            case 'Tools':
            case 'Buttons':
            case 'MenuItems':
                $update[$field] = $value;
                break;
            case 'Auth':
            case 'Login':
            case 'Menu':
            case 'Rank':
                $update[$field] = intval($value);
                break;

        }
        $r = !empty($update) ? $this->SysCurd->update($cond, $update) : false;
        // hook controller_zx_admin_sys_sys_dict_field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   批量更新字段
     * @rank    40
     */
    public function action_Update()
    {
        // hook controller_zxadmin_content_cate_disable_start.php
        $data = $this->request->param('data', []);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$data, $field], ['操作数据为空', '字段']);
        $field_fields = ['Auth', 'Login', 'Menu', 'Component'];
        !in_array($field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($field, SysCurdConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($field) {
            case 'Auth':
            case 'Login':
            case 'Menu':
                $update[$field] = intval($value);
                break;
            case 'Component':
                $update[$field] = $value;
                break;
        }

        $r = !empty($update) ? $this->SysCurd->updateByIndex($data, $update) : false;
        // hook controller_zxadmin_content_cate_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    // hook controller_zx_admin_sys_sys_curd_end.php
}

?>